package com.yanze.schoolinformation.adapter.rv;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.yanze.schoolinformation.R;
import com.yanze.schoolinformation.base.baseadapter.BaseRecyclerViewAdapter;
import com.yanze.schoolinformation.base.baseadapter.BaseRecyclerViewHolder;
import com.yanze.schoolinformation.databinding.ItemClearimageBinding;
import com.yanze.schoolinformation.model.bean.ChooseImageBean;
import com.yanze.schoolinformation.utils.PerfectClickListener;
import com.zhihu.matisse.Matisse;
import com.zhihu.matisse.MimeType;
import com.zhihu.matisse.engine.impl.GlideEngine;

/**
 * Created by MrZheng on 2018/5/9.
 */

public class NineImageAdapter extends BaseRecyclerViewAdapter<ChooseImageBean> {

    private Context mContext;
    private boolean isEdit = false;

    public NineImageAdapter(Context mContext) {
        this.mContext = mContext;
    }

    @NonNull
    @Override
    public BaseRecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(parent, R.layout.item_clearimage);
    }

    class ViewHolder extends BaseRecyclerViewHolder<ChooseImageBean,ItemClearimageBinding>{

        public ViewHolder(ViewGroup viewGroup, int layoutId) {
            super(viewGroup, layoutId);
        }

        @Override
        public void onBindViewHolder(ChooseImageBean bean, int position) {
            binding.executePendingBindings();
            if (bean.getType() == ChooseImageBean.TYPE_ADD) {
                showAdd();
            }else{
                showImage(bean.getUri(),position);
            }
        }

        private void showAdd() {
            binding.ivClear.setVisibility(View.GONE);
            Glide.with(mContext).load(R.mipmap.blackadd).dontAnimate().centerCrop().into(binding.ivImage);
            binding.ivImage.setOnClickListener(new PerfectClickListener() {
                @Override
                protected void onNoDoubleClick(View v) {
                    //点击发表
                    Matisse.from((Activity) mContext)
                            .choose(MimeType.allOf()) // 选择 mime 的类型
                            .countable(true)
                            .maxSelectable(9) // 图片选择的最多数量
                            .restrictOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)
                            .thumbnailScale(0.85f) // 缩略图的比例
                            .imageEngine(new GlideEngine()) // 使用的图片加载引擎
                            .forResult(222); // 设置作为标记的请求码
                }
            });
        }

        private void showImage(Uri uri, final int position) {
            Glide.with(mContext).load(uri).crossFade(500).centerCrop().into(binding.ivImage);

            //如果处在编辑模式,则显示 x 图标
            if (isEdit) {
                binding.ivClear.setVisibility(View.VISIBLE);
                binding.ivClear.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        data.remove(position);
                        //判断,如果移除后集合中没有 + 图标,则添加
                        boolean flag = true;
                        for (int i = 0; i < data.size(); i++) {
                            if (data.get(i).getType() == ChooseImageBean.TYPE_ADD) {
                                flag = false;
                                break;
                            }
                        }
                        if (flag) {
                            data.add(new ChooseImageBean(ChooseImageBean.TYPE_ADD, null));
                        }
                        notifyDataSetChanged();
                    }
                });
            } else {
                binding.ivClear.setVisibility(View.GONE);
            }
            //短按
            binding.ivImage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (isEdit) {
                        isEdit = false;
                        notifyDataSetChanged();
                    }
                }
            });

            //添加长按进入编辑模式
            binding.ivImage.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    isEdit = !isEdit;
                    notifyDataSetChanged();
                    return true;
                }
            });
        }
    }
}
